<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('business.harbor.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row>
                <el-col :span="8">
                  <el-form-item :label="i18n('business.harbor.code')" prop="code">
                    <el-input :placeholder="i18n('common.please_input') + i18n('business.harbor.code')"
                      v-model="dataForm.code" show-word-limit maxlength="10"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('business.harbor.chinese_name')" prop="chinese_name">
                    <el-input :placeholder="i18n('common.please_input') + i18n('business.harbor.chinese_name')"
                      v-model="dataForm.chinese_name" show-word-limit maxlength="50"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('business.harbor.english_name')" prop="english_name">
                    <el-input :placeholder="i18n('common.please_input') + i18n('business.harbor.english_name')" v-model="dataForm.english_name" show-word-limit maxlength="64"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="8">
                  <el-form-item :label="i18n('business.harbor.alpah_2_code')" prop="alpah_2_code">
                    <el-select v-model="dataForm.alpah_2_code" :placeholder="i18n('common.please_select') + i18n('business.harbor.alpah_2_code')" filterable clearable>
                      <el-option v-for="(v, k) in nationList" :label="v.chinese_name" :key="k" :value="v.alpah_2_code">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('business.harbor.chinese_short_name')" prop="chinese_short_name">
                    <el-input :placeholder="i18n('common.please_input') + i18n('business.harbor.chinese_short_name')" v-model="dataForm.chinese_short_name" show-word-limit maxlength="20"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('business.harbor.english_short_name')" prop="english_short_name">
                    <el-input :placeholder="i18n('common.please_input') + i18n('business.harbor.english_short_name')" v-model="dataForm.english_short_name" show-word-limit maxlength="48"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="8">
                  <el-form-item :label="i18n('business.harbor.chinese_alias')" prop="chinese_alias">
                    <el-input :placeholder="i18n('common.please_input') + i18n('business.harbor.chinese_alias')" v-model="dataForm.chinese_alias" show-word-limit maxlength="20"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('business.harbor.english_alias')" prop="english_alias">
                    <el-input :placeholder="i18n('common.please_input') + i18n('business.harbor.english_alias')" v-model="dataForm.english_alias" show-word-limit maxlength="48"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('common.sort')" prop="sort">
                    <el-input-number :placeholder="i18n('common.please_input') + i18n('common.sort')" v-model="dataForm.sort" :min="0"></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="8">
                  <el-form-item :label="i18n('business.harbor.shiproute_chinese_name')" prop="shiproute_chinese_name">
                    <el-input :placeholder="i18n('common.please_input') + i18n('business.harbor.shiproute_chinese_name')"
                      v-model="dataForm.shiproute_chinese_name" show-word-limit maxlength="32"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('business.harbor.shiproute_english_name')" prop="shiproute_english_name">
                    <el-input :placeholder="i18n('common.please_input') + i18n('business.harbor.shiproute_english_name')"
                      v-model="dataForm.shiproute_english_name" show-word-limit maxlength="32"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('business.harbor.area_id')">
                    <el-cascader class='cascader' v-model="dataForm.area_id" :placeholder="$t('common.please_select') + $t('business.supplier.area')" :options="areaList" :props="levelProps" clearable></el-cascader>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('common.status')" prop="status">
                    <el-switch
                      v-model="dataForm.status"
                      active-value="1"
                      :active-text="i18n('common.enable')"
                      inactive-value="2"
                      :inactive-text="i18n('common.disable')">
                    </el-switch>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button v-if="isAuth('module:business:harbor:handle')" type="primary" @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="business-harbor-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/harbor')

  const nationList = ref([])
  const shiprouteList = ref([])
  const areaList = ref([])

  const levelProps = ref({
    label: 'title',
    value: 'id',
    children: 'child',
    checkStrictly: false
  })

  const dataForm = reactive({
    id: 0,
    alpah_2_code: '',
    code: '',
    chinese_name: '',
    english_name: '',
    chinese_short_name: '',
    english_short_name: '',
    chinese_alias: '',
    english_alias: '',
    shiproute_chinese_name: '',
    shiproute_english_name: '',
    area_id: '',
    sort: 0,
    status: '1',
  })

  const dataRule = reactive({
    code: [{
      required: true,
      message: i18n('business.harbor.rules.code.require'),
      trigger: 'blur',
    }],
    chinese_name: [{
      required: true,
      message: i18n('business.harbor.rules.chinese_name.require'),
      trigger: 'blur',
    }],
    english_name: [{
      required: true,
      message: i18n('business.harbor.rules.english_name.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.alpah_2_code = data.data.alpah_2_code
            dataForm.code = data.data.code
            dataForm.chinese_name = data.data.chinese_name
            dataForm.english_name = data.data.english_name
            dataForm.chinese_short_name = data.data.chinese_short_name
            dataForm.english_short_name = data.data.english_short_name
            dataForm.chinese_alias = data.data.chinese_alias
            dataForm.english_alias = data.data.english_alias
            dataForm.shiproute_chinese_name = data.data.shiproute_chinese_name
            dataForm.shiproute_english_name = data.data.shiproute_english_name
            dataForm.area_id = data.data.area_id
            dataForm.sort = data.data.sort
            dataForm.status = data.data.status.value + ''
          }
        })
      }
    })
  }

  const loadNationList = () => {
    $http({
      url: $http.adornUrl('/business/nation/select'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        nationList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadShiprouteList = () => {
    $http({
      url: $http.adornUrl('/business/shiproute/select'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        shiprouteList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadAreaList = () => {
    $http({
      url: $http.adornUrl('/common/area/second'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        areaList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    init(formRef)

    loadNationList()
    loadShiprouteList()
    loadAreaList()
  })
</script>

<style lang="scss" scoped>
  .el-form-item {
    width: 100%;
  }
</style>
